package com.yager.controller;

import com.github.pagehelper.PageInfo;
import com.yager.model.House;
import com.yager.model.Page;
import com.yager.model.User;
import com.yager.service.CollectionService;
import com.yager.service.HouseService;
import com.yager.service.OrderService;
import com.yager.service.UserService;
import com.yager.util.DividePageUtil;
import com.yager.util.FilesUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.*;

@Controller
@Slf4j
@RequestMapping("/house")
public class HouseController {
    @Autowired
    private HouseService houseService;

    @Autowired
    private CollectionService collectionService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private UserService userService;

    @PostMapping("/insert")
    public String insertHouse(@RequestParam("houseImage") MultipartFile[] multipartFile, HttpServletRequest request, HttpSession session) {
        System.out.println("我是你爹");
        String houseid = null;
        String housetype = request.getParameter("housetype");
        String[] housefunitures = request.getParameterValues("housefuniture");
        Integer houseprice = Integer.parseInt(request.getParameter("houseprice"));
        Integer housesize = Integer.parseInt(request.getParameter("housesize"));
        Integer houseage = Integer.parseInt(request.getParameter("houseage"));
        String province = request.getParameter("province");
        String city = request.getParameter("city");
        String district = request.getParameter("district");
        String houseaddress = province + "-" + city + "-" + district;
        String housename = request.getParameter("housename");
        String housedescription = request.getParameter("housedescription");
        String userid = request.getParameter("");
        /*String userid = session.getAttribute("userid");*/
        Boolean housestatus = true;
        String housefuniture = " ";
        for (String str : housefunitures) {
            housefuniture = housefuniture +  str + " ";
            System.out.println(str);
        }
        House house = new House(houseid, housename, houseprice, userid, null, housetype, housesize, housedescription, housefuniture, houseage, housestatus, houseaddress);
        houseService.insertHouse(multipartFile, house);
        return "house/homePage";
    }

    @GetMapping("/delete")
    public String deleteHouse(String houseid) {
        log.info("正在删除请稍后");
        houseService.deleteHouse(houseid);
        log.info(houseid+"删除成功");
        collectionService.removeCollectionByHouseId(houseid);
        log.info("房子的相关收藏已经移除");
        orderService.removeOrderByHouseId(houseid);
        return "house/myHouses";
    }

    @GetMapping("/select/conditions/{housetype}/{houseaddress}/{sortage}/{sortprice}")
    public @ResponseBody
    List<House> selectHouseByConditions(@PathVariable String housetype, @PathVariable String houseaddress, @PathVariable boolean sortage, @PathVariable boolean sortprice, Model model) {
        log.info("准备查询");
        List<House> houseList = houseService.selectHouseByConditions(housetype, houseaddress, sortage, sortprice);
        model.addAttribute("houses", houseList);
        log.info("查询完成");
        return houseList;
    }

    @GetMapping("/updatehouse")
    public void updateHouse(HttpServletRequest request /*@RequestParam("houseImage") MultipartFile multipartFile*/) {
        String houseid = request.getParameter("houseid");
        String housetype = request.getParameter("housetype");
        System.out.println(housetype);
        String[] housefunitures = request.getParameterValues("housefuniture");
        Integer houseprice = Integer.parseInt(request.getParameter("houseprice"));
        Integer housesize = Integer.parseInt(request.getParameter("housesize"));
        Integer houseage = Integer.parseInt(request.getParameter("houseage"));
        String province = request.getParameter("province");
        String city = request.getParameter("city");
        String district = request.getParameter("district");
        String houseaddress = province + "-" + city + "-" + district;
        String housename = request.getParameter("housename");
        String housedescription = request.getParameter("housedescription");
        String userid = null;
        Boolean housestatus = true;
        String housefuniture = " ";
        for (String str : housefunitures) {
            housefuniture = housefuniture + str + " ";
            System.out.println(str);
        }
        House house = new House(houseid, housename, houseprice, userid, null, housetype, housesize, housedescription, housefuniture, houseage, housestatus, houseaddress);
        houseService.updateHouse(house);
    }

    @GetMapping("/select/textandconditions/{housetext}/{housetype}/{houseaddress}/{sortage}/{sortprice}")
    @ResponseBody
    public List<House> selectHouseByTextAndConditions(@PathVariable String housetext, @PathVariable String housetype, @PathVariable String houseaddress, @PathVariable int sortage, @PathVariable int sortprice) {
        List<House> houseList = houseService.selectHouseByTextAndConditions(housetext, housetype, houseaddress, sortage, sortprice);
        return houseList;
    }

    @GetMapping("/select/primary")
    public void selectHouseByPrimaryKey(String houseid, HttpSession session) {
        House house = houseService.selectHouseByPrimaryKey(houseid);
        List<String> imageList = new ArrayList<String>();
        String dirLocalPath = house.getHouseimages();
        File[] files = FilesUtil.getAllFile(dirLocalPath);
        for(int i = 0; i < files.length; i ++){
            String s = files[i].getPath();
            s = s.substring(s.lastIndexOf("h"), s.length());
            imageList.add(s);
        }
        session.setAttribute("Onehouse", house);
        session.setAttribute("houseimage", imageList);
    }

   /* @GetMapping("/select/user")
    public String selectHouseByUserId(HttpSession session){
        User user = (User) session.getAttribute("user");
        String userid = user.getUserid();
        List<House> houses = houseService.selectHouseByUserId(userid);
        System.out.println("芜湖起飞");
        List<String> imageList = new ArrayList<String>();
            for (int i = 0; i < houses.size(); i++){
            House house = houses.get(i);
            String dirLocalPath = house.getHouseimages();
            System.out.println("doushi didi ");
            File[] files = FilesUtil.getAllFile(dirLocalPath);
            String a = files[0].getPath();
            a = a.substring(a.lastIndexOf("h"),a.length());
            imageList.add(a);
        }
            session.setAttribute("houses", houses);
            session.setAttribute("images", imageList);
        return "house/homePage :: houseList";
    }*/

    @RequestMapping("/to_houseDetail")
//    @ResponseBody
    public String toHouseInfo(String houseid,HttpSession session){
//        String houseid = request.getParameter("houseid");
//        System.out.println("houseid:"+houseid);
        House house = houseService.selectHouseByPrimaryKey(houseid);
        System.out.println("house:"+house);
        List<String> imageList = new ArrayList<String>();
        String dirLocalPath = house.getHouseimages();
        File[] files = FilesUtil.getAllFile(dirLocalPath);
        for(int i = 0; i < files.length; i ++){
            String s = files[i].getPath();
            s = s.substring(s.lastIndexOf("h"), s.length());
            imageList.add(s);
        }

        User user=(User) session.getAttribute("user");
        if(user==null){
            session.setAttribute("collected","收藏");
        }
        else if (collectionService.isThisCollected(user.getUserid(),houseid)) {
            session.setAttribute("collected","已收藏");
        }
        else {
            session.setAttribute("collected","收藏");
        }
        User owner=userService.getUserById(house.getUserid());

        session.setAttribute("houseOwner",owner);
        session.setAttribute("Onehouse", house);
        session.setAttribute("houseimage", imageList);
        return "house/houseDetail";
    }
    @RequestMapping("/to_updateHouse")
    public String toUpdateHouse(String houseid, HttpSession session){
        House house = houseService.selectHouseByPrimaryKey(houseid);
        System.out.println(house);
        session.setAttribute("Onehouse", house);
        return "house/updateHouse";
    }
    @RequestMapping("to_findHouses")
    public String toFindHouses(HttpSession session, @RequestParam(defaultValue = "1") Integer pageNo){
        PageInfo<House> houses = houseService.selectHouseOrderById(pageNo);
        List<House> houseList = houses.getList();
        List<String> imageList = new ArrayList<String>();
        for (int i = 0; i < houseList.size(); i++) {
            House house = houseList.get(i);
            String dirLocalPath = house.getHouseimages();
            File[] files = FilesUtil.getAllFile(dirLocalPath);
            String a = files[0].getPath();
            a = a.substring(a.lastIndexOf("h"),a.length());
            imageList.add(a);
        }
        session.setAttribute("houses", houseList);
        session.setAttribute("images", imageList);
        PageInfo<String> images = new PageInfo<>(imageList);
        session.setAttribute("housepage", houses);
        session.setAttribute("imagepage", images);
        return "house/findHouses";
    }
    @RequestMapping("/to_findHousesByText")
    public String textHouses(HttpSession session, String housetext, @RequestParam(defaultValue = "1") Integer pageNo){
        PageInfo<House> houseByText = houseService.selectHouseByText(housetext, pageNo);
        List<House> houseList = houseByText.getList();
        List<String> imageList = new ArrayList<String>();
        for (int i = 0; i < houseList.size(); i++) {
            House house = houseList.get(i);
            String dirLocalPath = house.getHouseimages();
            File[] files = FilesUtil.getAllFile(dirLocalPath);
            String a = files[0].getPath();
            a = a.substring(a.lastIndexOf("h"),a.length());
            imageList.add(a);
        }
        session.setAttribute("houses", houseList);
        session.setAttribute("images", imageList);
        PageInfo<String> images = new PageInfo<>(imageList);
        session.setAttribute("housepage",houseByText);
        session.setAttribute("imagepage",images);
        return "house/textHouses";
    }
    @RequestMapping("/to_addHouse")
    public String toAddHouse(HttpSession session){
        return "house/addhouse";
    }

    @RequestMapping("/to_myHouses")
    public String toMyHouses(HttpSession session,@RequestParam(defaultValue = "1") Integer pageNo)
    {
        User user = (User) session.getAttribute("user");
        String userid = user.getUserid();
        PageInfo<House> housePageInfo = houseService.selectHouseByUserId(userid, pageNo);
        List<House> houseList = housePageInfo.getList();
        List<String> imageList = new ArrayList<String>();
        for (int i = 0; i < houseList.size(); i++){
            House house = houseList.get(i);
            String dirLocalPath = house.getHouseimages();
            File[] files = FilesUtil.getAllFile(dirLocalPath);
            String a = files[0].getPath();
            a = a.substring(a.lastIndexOf("h"),a.length());
            imageList.add(a);
        }
        session.setAttribute("houses", houseList);
        session.setAttribute("images", imageList);
        PageInfo<String> images = new PageInfo<>(imageList);
        session.setAttribute("housepage", housePageInfo);
        session.setAttribute("imagepage", images);
        return "house/myHouses";
    }
    @RequestMapping("/to_home")
    public String toHome(HttpSession session){
        PageInfo<House> houses = houseService.selectHouseOrderById(1);
        List<House> houseList = houses.getList();
        List<String> imageList = new ArrayList<String>();
        for (int i = 0; i < houseList.size(); i++) {
            House house = houseList.get(i);
            String dirLocalPath = house.getHouseimages();
            File[] files = FilesUtil.getAllFile(dirLocalPath);
            String a = files[0].getPath();
            a = a.substring(a.lastIndexOf("h"),a.length());
            imageList.add(a);
        }
        PageInfo<String> images = new PageInfo<>(imageList);
        session.setAttribute("homehousepage", houses);
        session.setAttribute("homeimagepage", images);
        return "house/home";
    }
}
